package com.example.hospital.patient.wx.api.utils;

import com.example.hospital.patient.wx.api.exception.HospitalException;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.web.multipart.MultipartFile;

public class UploadMinio {
    private static String endpoint = "http://114.132.57.246:9000";

    private static String patientUrl = "http://114.132.57.246:9000/hospital";

    private static String accessKey = "root";

    private static String secretKey = "abc123456";

    public static String uploadImage(MultipartFile file,String url,Integer id){
        String filename = url + "-" + id  + ".jpg" ;
        String fold = url + "/";
        MinioClient client = new MinioClient.Builder().endpoint(endpoint)
                .credentials(accessKey, secretKey).build();
        try {
            client.putObject(PutObjectArgs.builder().bucket("hospital")
                    .object(fold + filename)
                    .stream(file.getInputStream(), -1, 5 * 1024 * 1024)
                    .contentType("image/jpeg").build());

            return patientUrl + "/"+fold+filename;
        }catch (Exception e){
            System.out.println("保存失败");
            throw new HospitalException("保存分类图片失败");
        }
    }
}